using Unity.Entities;
using UnityEngine;

namespace Tanks.Step2
{
    public struct Turret : IComponentData
    {
        /// <summary>
        /// 该实体用于引用Prefab，之后可用其来实例化Prefab
        /// </summary>
        public Entity CannonBallPrefab;

        /// <summary>
        /// 炮弹发射点
        /// </summary>
        public Entity CannonBallSpawn;
    }

    /// <summary>
    /// 对于开关功能使用 IEnableableComponent 将更高效，避免了增删组件产生的开销
    /// </summary>
    public struct Shooting : IComponentData, IEnableableComponent
    {
    }

    public class TurretAuthoring : MonoBehaviour
    {
        public GameObject CannonBallPrefab;
        public Transform CannonBallSpawn;

        class Baker : Baker<TurretAuthoring>
        {
            public override void Bake(TurretAuthoring authoring)
            {
                var entity = GetEntity(TransformUsageFlags.Dynamic);
                AddComponent(entity, new Turret
                {
                    CannonBallPrefab = GetEntity(authoring.CannonBallPrefab, TransformUsageFlags.Dynamic),
                    CannonBallSpawn = GetEntity(authoring.CannonBallSpawn, TransformUsageFlags.Dynamic)
                });
                AddComponent(entity, new Shooting());
            }
        }
    }
}